﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace myTunes.Test
{
    /// <summary>
    /// Summary description for CustomTests
    /// </summary>
    [TestClass]
    public class CustomTests
    {
        public CustomTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        //[TestMethod]
        public void TestMethod1()
        {
            Console.WriteLine(
    "This example of TimeSpan.Parse( string ) and \n" +
    "TimeSpan.ToString( ) " +
    "generates the following output.\n");
            Console.WriteLine("{0,20}   {1,21}",
                "String to Parse", "TimeSpan or Exception");
            Console.WriteLine("{0,20}   {1,21}",
                "---------------", "---------------------");

            ParseNDisplayTimeSpan("0");
            ParseNDisplayTimeSpan("14");
            ParseNDisplayTimeSpan("1:2:3");
            ParseNDisplayTimeSpan("0:0:0.250");
            ParseNDisplayTimeSpan("10.20:30:40.50");
            ParseNDisplayTimeSpan("99.23:59:59.9999999");
            ParseNDisplayTimeSpan("0023:0059:0059.0099");
            ParseNDisplayTimeSpan("24:0:0");
            ParseNDisplayTimeSpan("0:60:0");
            ParseNDisplayTimeSpan("0:0:60");
            ParseNDisplayTimeSpan("10:");
            ParseNDisplayTimeSpan(":10");
            ParseNDisplayTimeSpan("10:20:");
            ParseNDisplayTimeSpan(".123");
            ParseNDisplayTimeSpan("10.");
            ParseNDisplayTimeSpan("10.12");
        }

        void ParseNDisplayTimeSpan(string intervalStr)
        {
            // Write the first part of the output line.
            Console.Write("{0,20}   ", intervalStr);

            // Parse the parameter, and then convert it back to a string.
            try
            {
                TimeSpan intervalVal = TimeSpan.Parse(intervalStr);
                string intervalToStr = intervalVal.ToString();

                // Pad the end of the TimeSpan string with spaces if it 
                // does not contain milliseconds.
                int pIndex = intervalToStr.IndexOf(':');
                pIndex = intervalToStr.IndexOf('.', pIndex);
                if (pIndex < 0) intervalToStr += "        ";

                Console.WriteLine("{0,21}", intervalToStr);
            }
            catch (Exception ex)
            {
                // If Parse throws an exception, write the message.
                Console.WriteLine(ex.Message);
            }
        }

        [TestMethod]
        public void DurationToTime_Test()
        {
            string result = GetTimeFromDuration(124);
            Assert.AreEqual(result, "2:04");
            result = GetTimeFromDuration(0);
            Assert.AreEqual(result, "0:00");
            result = GetTimeFromDuration(5);
            Assert.AreEqual(result, "0:05");
            result = GetTimeFromDuration(60);
            Assert.AreEqual(result, "1:00");
            result = GetTimeFromDuration(120);
            Assert.AreEqual(result, "2:00");
        }

        private string GetTimeFromDuration(int duration)
        {
            int rest;
            long d = Math.DivRem(duration, 60, out rest);
            return string.Format("{0}:{1}", d, rest.ToString().PadLeft(2, '0'));
        }
    }
}
